// pages/writeform/index.js
var app =getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		title:'', /// 标题
		imgList: [], // 图片列表
		textareaBValue:'',  //内容

		picker:[

			{
				id: 1, 
				text:'楼盘动态'
			},
			{
				id: 2, 
				text: '新闻资讯'
			},
			{
				id: 3, 
				text: '我的原创'
			},
			{
				id: 4, 
				text: '我的日记'
			},
			{
				id: 5, 
				text: '热点小事件'
			},
			{
				id: 6, 
				text: '小道消息'
			},

		], 
		selecttype:'楼盘动态', 
		selectid:1, // 选择的type
		imgarr:'',
	},
	pickerChange(e){

		var i = e.detail.value;
	
		this.setData({
			selecttype:this.data.picker[i].text,
			selectid: this.data.picker[i].id,
		})
		console.log(this.data.selecttype)
	}, // 选择type
	/**
	 * 生命周期函数--监听页面加载
	 */
	bindtitle(e){
		this.setData({
			title: e.detail.value
		})
	},// 获取title

	fabu(){
		var that = this;
		var title= that.data.title; // 标题
		var category = that.data.selectid;// 类型
		var photo_url = that.data.imgList; // 图片
		var content = that.data.textareaBValue;//内容
    var opid = app.globalData.openid;
		var imgarr = wx.getStorageSync('imgarr')
    var m='';
    if(imgarr){
       m = imgarr.join(',')
    }
	

		if(title == ""){
			wx.showToast({
				title: '请输入标题',
				icon: 'none',
				duration: 1000
			})
		}
		else if (content == ""){
			wx.showToast({
				title: '请输入内容',
				icon: 'none',
				duration: 1000
			})
		}
    // else if (m == "") {
    //   wx.showToast({
    //     title: '请上传图片',
    //     icon: 'none',
    //     duration: 1000
    //   })
    // }
		else{
			wx.showLoading({
				title: '发布中',
			})
			app.core('post', "create_article", {
				open_id: opid,
				title:title,
				category: category,
				album: m,
				content: content
			},(res=>{
				console.log(res);
					if (res.resultCode ==200){
						wx.showToast({
							title: '发布成功',
							icon: 'none',
							duration: 1000
						})
						setTimeout(function () {

							wx.switchTab({
								url: '/pages/write/index',
							});

						}, 1000)
						wx.hideLoading()
					}else{
						wx.showToast({
							title: '上传失败',
							icon: 'none',
							duration: 1000
						})
						wx.hideLoading()
					}
		
			}),
			(err=>{
				wx.showToast({
					title: '网络错误', 
					icon: 'none',
					duration: 1000
				})
				wx.hideLoading()
			}))
		}

	},// 发布写一写

	uploadimg(imglist){
		var that = this;
		var photo_url= that.data.imgList;
		var imgarr =[];
		console.log(imglist,'imglist')
		for (var i = 0; i < imglist.length; i++){
			wx.showLoading({
				title: '上传中',
			});
			wx.uploadFile({
				url: 'https://xkcloude.hfhouse.com/routineclient/upload_file',
				filePath: photo_url[i],
				name: 'photo_url',
				success(res) {
					var x = JSON.parse(res.data);
					console.log(x,'xxxx')
					imgarr.push(x.data.album[0]);
					wx.setStorageSync('imgarr', imgarr)
					console.log(imgarr)
					wx.hideLoading()
				}

			})
		
		}

	},


	onLoad: function (options) {
		var userInfo = wx.getStorageSync('userInfo');
		console.log(userInfo);
	
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
	ChooseImage() {
		wx.chooseImage({
			count: 4, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: (res) => {
				if (this.data.imgList.length != 0) {
					this.setData({
						imgList: this.data.imgList.concat(res.tempFilePaths)
					})
					this.uploadimg(this.data.imgList.concat(res.tempFilePaths))
		
				} else {
					this.setData({
						imgList: res.tempFilePaths
					})
					this.uploadimg(res.tempFilePaths)
				}
				console.log(res)
			}
		
		});


	},
	ViewImage(e) {
		wx.previewImage({
			urls: this.data.imgList,
			current: e.currentTarget.dataset.url
		});
	},
	DelImg(e) {
		wx.showModal({
			title: '提醒',
			content: '确定要删除这张照片吗？',
			cancelText: '取消',
			confirmText: '确定',
			success: res => {
				if (res.confirm) {
					this.data.imgList.splice(e.currentTarget.dataset.index, 1);
					this.setData({
						imgList: this.data.imgList
					})
				}
			}
		})
	},

	textareaBInput(e) {
		this.setData({
			textareaBValue: e.detail.value
		})
	},

})